import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
import router from './router';
import axios from 'axios';
import watermark from 'watermark-dom';
/* 引入公共函数 */
import common from './assets/js/common';
import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(common);
Vue.use(Vant);
Vue.use(ElementUI);
Vue.config.productionTip = false;
Vue.prototype.$watermark = watermark;
Vue.prototype.$http = axios;
// axios.defaults.baseURL = 'http://localhost:8080';
// axios.defaults.baseURL = 'http://192.168.30.244:8080';
// axios.defaults.baseURL = 'http://49.235.144.147:8080';
axios.defaults.baseURL = 'http://116.236.42.99:7000';

//设置axios请求头加入token
axios.interceptors.request.use(
  config => {
    if (config.push !== '/') {
      if (localStorage.getItem('token')) {
        //在请求头加入token，名字要和后端接收请求头的token名字一样
        config.headers.token = localStorage.getItem('token');
      }
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
//=============================
//响应回来token是否过期
axios.interceptors.response.use(
  response => {
    // console.log('响应回来：' + response.data.code)
    //和后端token失效返回码约定403
    if (response.data.code == 403) {
      // 引用elementui message提示框
      ElementUI.Message({
        message: '身份已失效',
        type: 'err'
      });
      //清除token
      localStorage.removeItem('token');
      //跳转
      router.push({
        name: 'login'
      });
    } else {
      return response;
    }
  },
  error => {
    return Promise.reject(error);
  }
);
var onPlusReady = function (callback, context = this) {
  if (window.plus) {
    callback.call(context);
  } else {
    document.addEventListener('plusready', callback.bind(context));
  }
};
Vue.mixin({
  beforeCreate() {
    onPlusReady(() => {
      this.plusReady = true;
    }, this);
  },
  methods: {
    onPlusReady: onPlusReady
  }
});
new Vue({
  router,
  render: h => h(App)
}).$mount('#app');
